<template>
  <div class="details">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="darkred">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" width="100%" alt="" />
      </van-swipe-item>
    </van-swipe>
    <Tips />
    <div class="info">
      <h2>{{ info.name }}</h2>
      <section>{{ info.goods_brief }}</section>
      <div>{{ info.retail_price | filterMoney }}</div>
    </div>
    <van-cell title="展示弹出层" is-link @click="ifShowModal = true"></van-cell>
    <div class="canshu">
      <h2>商品参数</h2>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <span>{{ item.name }}</span>
          <big>{{ item.value }}</big>
        </li>
      </ul>
    </div>
    <div class="tupian" v-html="info.goods_desc"></div>
    <div class="issue">
      <div class="line_title">
        <span></span>
        <h3>常见问题</h3>
      </div>
      <ul class="content">
        <li v-for="item in issue" :key="item.id">
          <h3>{{ item.question }}</h3>
          <p>{{ item.answer }}</p>
        </li>
      </ul>
    </div>
    <div class="alllook">
      <div class="line_title">
        <span></span>
        <h3>大家都在看</h3>
      </div>
      <ProductList :goodsList="goodsList" />
    </div>
    <transition name="fade">
      <div class="modal" v-show="ifShowModal">
        <div class="modal_bg" @click="ifShowModal = false"></div>
        <div class="modal_content">
          <div class="top">
            <img :src="info.list_pic_url" alt="" width="100" height="100" />
            <div class="top_right">
              <section>价格：{{ info.retail_price | filterMoney }}</section>
              <section>库存：{{ info.goods_number }}</section>
            </div>
          </div>
          <div class="down">
            <section>数量：</section>
            <van-stepper
              v-model="count"
              integer
              min="1"
              :max="info.goods_number"
              @change="onChange"
            />
          </div>
          <van-icon
            name="cross"
            class="cross"
            size="20"
            @click="ifShowModal = false"
          />
        </div>
      </div>
    </transition>
    <van-goods-action>
      <van-goods-action-icon icon="star-o" text="未收藏" />
      <van-goods-action-icon
        icon="cart-o"
        :badge="badge"
        text="购物车"
        @click="toCartFn"
      />
      <van-goods-action-button type="danger" text="立即购买" @click="toBuyFn" />
      <van-goods-action-button
        @click="ShowModalFn"
        type="warning"
        text="加入购物车"
      />
    </van-goods-action>
  </div>
</template>
 
<script>
import {
  GoodsDetailApi,
  GetAlllookApi,
  GoodsCountApi,
  AddToCartApi,
} from "../request/api";
import Tips from "../components/Tips";
import ProductList from "../components/ProductList";
export default {
  data() {
    return {
      ifShowModal: false,
      // 轮播图数组
      gallery: [],
      // 产品信息
      info: [],
      attribute: [],
      issue: [],
      goodsList: [],
      badge: 0,
      // 步进器中的数
      count: 1,
      productList: [],
    };
  },
  methods: {
    // 跳转到购物车
    toCartFn() {
      this.$router.push("/cart");
    },
    onChange(value) {},

    // 点击加入购物车
    ShowModalFn() {
      if (this.ifShowModal) {
        let token = localStorage.getItem("X-Nideshop-Token");
        if (token) {
          // modal已经打开，加入购物车
          AddToCartApi({
            goodsId: this.$route.params.id,
            productId: this.productList[0].id,
            number: this.count,
          }).then((res) => {
            if (res.errno == 0) {
              // 加入购物车成功
              this.$toast.success("添加成功");
              // 关闭模态框
              this.ifShowModal = false;
              console.log(res.data);
              // 重新渲染badge
              this.badge = res.data.cartTotal.goodsCount;
              console.log(res.data.cartTotal);
              // console.log(this.badge);
            }
          });
        } else {
          // 显示一个即将跳转的toast
          this.$toast.loading({
            message: "请先登录...",
            forbidClick: true,
          });
          setTimeout(() => {
            // 没有token，跳去登录页
            this.$router.push("/user");
          }, 1500);
        }
      } else {
        this.ifShowModal = true;
      }
    },

    // 点击立即购买
    toBuyFn() {
      this.$toast("该功能暂未开通");
    },
  },
  async created() {
    // console.log(this.$route);
    // 获取当前产品的数据
    await GoodsDetailApi({
      id: this.$route.params.id,
    }).then((res) => {
      console.log(res);
      if (res.errno == 0) {
        let { attribute, gallery, info, issue, productList } = res.data;
        this.gallery = gallery;
        this.info = info;
        console.log(this.info);
        this.attribute = attribute;
        this.issue = issue;
        this.productList = productList;
      }
    });
    // 获取购物车数量
    await GoodsCountApi().then((res) => {
      if (res.errno == 0) {
        this.badge = res.data.cartTotal.goodsCount;
      }
    });
    // 获取相关产品的数据
    await GetAlllookApi({
      id: this.$route.params.id,
    }).then((res) => {
      if (res.errno == 0) {
        console.log(res);
        let { goodsList } = res.data;
        this.goodsList = goodsList;
      }
    });
  },
  components: {
    Tips,
    ProductList,
  },
  watch: {
    $route(to, from) {
      if (to.path == from.path) return;
      this.$router.go(0); // 刷新页面
    },
  },
};
</script>
 
<style lang = "less">
.details {
  padding-bottom: 60px;
}
.my-swipe .van-swipe-item {
  line-height: 0;
}
.info {
  background-color: #fff;
  text-align: center;
  padding: 20px 0;
  border-bottom: 1px solid #ccc;
  h2,
  section,
  div {
    height: 40px;
    line-height: 40px;
  }
  h2 {
    font-size: 20px;
  }
  section {
    color: #666;
  }
  div {
    color: darkred;
  }
}
.van-cell {
  margin-bottom: 20px;
}

.canshu {
  padding: 10px;
  background-color: #fff;
  h2 {
    font-size: 20px;
    line-height: 40px;
  }
  ul {
    li {
      background-color: #efefef;
      border-radius: 6px;
      height: 35px;
      align-items: center;
      display: flex;
      justify-content: space-between;
      margin-bottom: 8px;
      span {
        color: #999;
        width: 25%;
        margin-right: 5px;
        text-align: right;
      }
      big {
        flex: 1;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
  }
}
/* 深度修改样式 /deep/ */
.tupian {
  img {
    width: 100%;
    display: block;
  }
}

.line_title {
  width: 100%;
  height: 40px;
  position: relative;
  background-color: #fff;
  span {
    width: 50%;
    height: 2px;
    background-color: #ccc;
    position: absolute;
    top: 20px;
    left: 25%;
    margin: 0;
  }
  h3 {
    width: 30%;
    height: 40px;
    line-height: 40px;
    background-color: #fff;
    position: absolute;
    top: 0;
    left: 35%;
    margin: 0;
    text-align: center;
    font-size: 18px;
  }
}
.issue {
  background-color: #fff;
  .content {
    h3 {
      padding-left: 20px;
      height: 30px;
      line-height: 30px;
      position: relative;
      font-size: 16px;
      padding-right: 10px;
      margin-bottom: 10px;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background-color: darkred;
        border-radius: 50%;
        position: absolute;
        left: 8px;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      padding-left: 20px;
      padding-right: 10px;
      font-size: 12px;
      padding-bottom: 10px;
      color: #666;
    }
  }
}
.alllook {
  background-color: #fff;
}
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .modal_bg {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
  }
  .modal_content {
    width: 100%;
    min-height: 30vh;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 50px;
    padding: 10px;
    box-sizing: border-box;
    .top {
      /* background-color: darksalmon; */
      display: flex;
      height: 100px;
      .top_right {
        flex: 1;
        /* background-color: darkseagreen; */
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        padding-left: 10px;
      }
    }
    .down {
      section {
        font-size: 20px;
        margin-top: 20px;
        margin-bottom: 20px;
      }
    }
    .cross {
      position: absolute;
      right: 10px;
      top: 10px;
    }
  }
}
</style>